clear;
clc;
close all;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates figure 2 in the paper 
%------------------------------------------------------------------------%


% load ----------------------------------------------------------------------- 

file_name = '../01_data/es_quotes_5minute_EST';

% Call returns routine -----------------------------------------------------------------------

start_date     = datenum(1998,1,5);
finish_date    = datenum(2020,12,31);

winsorizing    = 0;
alpha          = 0.99;
replace_option = 0;

results         = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
return_AD       = results.return_AD;
return_ON       = results.return_ON;
return_ID       = results.return_ID;
log_return_AD   = results.log_return_AD;
log_return_ON   = results.log_return_ON;
log_return_ID   = results.log_return_ID;
cum_log_returns = results.cum_log_returns;
caldt_mat       = results.caldt_mat;
T               = results.T;

% LOG RETURNS -----------------------------------------------------------------------

mu_log_return_AD        = 100*252*nanmean(log_return_AD,1);          
mu_log_return_ON        = 100*252*nanmean(log_return_ON,1);         
mu_log_return_ID        = 100*252*nanmean(log_return_ID,1);

% mean cum log returns and standard error across intervals
mu_cum_log_returns      = 100*252*[0 , nanmean(cum_log_returns,1)];
var_cum_log_returns     = [NaN ,nanvar(100*252*cum_log_returns,1)];
se_cum_log_returns      = sqrt(var_cum_log_returns/T);

% annualized mean of HbH log return
mu_log_return_AD_HbH    = 100*252*nanmean(results.log_return_HbH_AD);

% SIMPLE RETURNS -----------------------------------------------------------------------

mu_return_AD                    = 100*((nangeomean(1+return_AD,1)).^252-1);
mu_return_ON                    = 100*((nangeomean(1+return_ON,1)).^252-1);
mu_return_ID                    = 100*((nangeomean(1+return_ID,1)).^252-1);

% annualized mean of HbH simple return
mu_return_AD_HbH                = 100*((nangeomean(1+results.return_HbH_AD,1)).^252-1);

% mean cumulative investment to 1 dollar
mu_cum_returns_one_dollar_inv   = [1 , nangeomean(results.cum_returns,1)].^(252);
se_cum_returns_one_dollar_inv   = se_cum_log_returns/100;

% colour in special hours
special_hours                   = zeros(1,24);
special_hours(1,9)              = mu_log_return_AD_HbH(1,9);
special_hours(1,16)             = mu_log_return_AD_HbH(1,16);

% ---------------------------------------------------------------------------------- %

% combine hourly plot and cumulative plot 


hour_index_1        = 1 : 289;
x_ticks_1           = 1 : 12 : 289;
hour_string_1       = {'18', '19', '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04' , '05' , '06' , '07' , '08' , '09' , '10' , '11' , '12' , '13', '14' , '15' , '16', '17' , '18'};

hour_index_2        = 1 : 24;
x_ticks_2           = 1 : 24;
hour_string_2       = {'18', '19', '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04' , '05' , '06' , '07' , '08' , '09' , '10' , '11' , '12' , '13', '14' , '15' , '16', '17' , '18'};


lightblue           = [91, 207, 244] / 255; 
lightgrey           = [0.7  0.7  0.7];

sig_level           = 1.69;
FS                  = 18;
LW                  = 5;

CTC = mu_cum_log_returns(end)

AfterRet = CTC - mu_cum_log_returns(268)

CTO = mu_cum_log_returns(187) + AfterRet
OTC = CTC - CTO

mu_cum_log_returns(268) - mu_cum_log_returns(187)

OD = mu_log_return_AD_HbH(1,9)
OR = mu_log_return_AD_HbH(1,16)

figure
p=bar(hour_index_2, mu_log_return_AD_HbH, 1, 'k');
set(p,'FaceColor', lightgrey);
hold on 
p=bar(hour_index_2, special_hours, 1, 'k');
set(p,'FaceColor', lightgrey);
hold on
ylabel('avg. log returns (% p.a)')
hold on
xlim([hour_index_2(1)-0.5 hour_index_2(end)+0.5])
ylim([-0.1 1]*10)
xtickangle(90)
set(gca,'XTick',x_ticks_2,'XTickLabel',hour_string_2);
hold on
ax1 = gca;
set(ax1,'XTick',[])
set(gca, 'layer', 'bottom');
grid on
legend off
hold on
ax2_pos = ax1.Position; 
ax2 = axes('Position',ax2_pos,'XAxisLocation','bottom','YAxisLocation','right', 'Color','none');
hold on 
l1 = line([hour_index_1(1) , hour_index_1(end)] ,[mu_cum_log_returns(1) , mu_cum_log_returns(end)]); 
l1.LineWidth = LW;
l1.Color = 'red';
hold on
l2 = line(hour_index_1,mu_cum_log_returns); 
l2.LineWidth = LW;
l2.Color = 'black';
xlim([1 289])
ylim([-0.1 1]*10)
set(gca,'XTick',x_ticks_1,'XTickLabel',hour_string_1);
hold on 
xlabel('Trade Time (ET)')
legend off
